package com.lcxw.hxzbapi.tencentim.controller;

import com.alibaba.fastjson.JSONObject;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbapi.tencentim.FeignService.ImManager;
import com.lcxw.hxzbapi.tencentim.common.TencentImConfig;
import com.tencentyun.TLSSigAPIv2;
import java.util.Random;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/im")
public class TestController {
    @Resource
    ImManager imManager;

    @Resource
    private TencentImConfig configBean;

    @PostMapping("/import")
    public Response importAccount(@RequestBody JSONObject requestParams){

        TLSSigAPIv2 tlsSigAPIv2 = new TLSSigAPIv2(configBean.getAppid(),configBean.getKey());

        Random r = new Random();
        long random = r.nextLong();
        String userId = requestParams.getString("userId");
        String admin = configBean.getAdmin();
        String key = tlsSigAPIv2.genSig(admin,configBean.getExpire());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("Identifier",userId);
        String s = imManager
            .importAccount(configBean.getAppid(), admin, key, random, jsonObject);

        return Response.ok();
    }
}
